import optparse
import nmap

pr_blue = '\033[94m'
pr_red = '\033[31m'
pr_yellow = '\033[93m'
pr_green = '\033[96m'

def main() :
    paser = optparse.OptionParser(
        "usages%prog" + "-H <host> -p <port>"
    )

    paser.add_option("-H", dest="tgtHost", type="string", help="specify host")
    paser.add_option("-p", dest="tgtPort", type="string", help="port")

    (options, args) = paser.parse_args()
    tgtHost = options.tgtHost

    tgtPorts = str(options.tgtPort).split(",")

    if (tgtHost == None) | (tgtPorts[0] == None):
        print(paser.usage)
        exit(0)
    
    for tgtPort in tgtPorts:
        nmapScan(tgtHost, tgtPort)

def nmapScan(tgtHost, tgtPort):
    nmScan = nmap.PortScanner()
    nmScan.scan(tgtHost, tgtPort)
    state = nmScan[tgtHost]["tcp"][int(tgtPort)]["state"]
    protocol = nmScan[tgtHost]["tcp"][int(tgtPort)]["name"]
    product = nmScan[tgtHost]["tcp"][int(tgtPort)]["product"]
    version = nmScan[tgtHost]["tcp"][int(tgtPort)]["version"]
    extrainfo = nmScan[tgtHost]["tcp"][int(tgtPort)]["extrainfo"]

    result = "%s[*] %s%s tcp/%s %s[%s]\n%s%s %s" % ( pr_red, pr_blue, tgtHost, tgtPort, pr_green, state, pr_yellow, product, version)

    if extrainfo:
        result += "os: %s" % extrainfo
    
    print(result)

